﻿<%@ Page Language="C#" %>
<%@ Import Namespace="IRL.Utilities" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.SqlClient" %>
<script runat="server">
    public const int CURRENT_MODULE = 1;

    Hashtable ptype_lookup, rcat_lookup;
    StringBuilder menu_structure;

    protected void Page_Load(object sender, EventArgs e)
    {
        Security SysAcc = new Security(CURRENT_MODULE);
        // check is authenticate
        SysAcc.Authenticate();

        // load reminder count  
        //Mics.loadReminderCount(lblReminderCount);

        InitLookUp();
        if (IsPostBack)
        {
            Context.Items.Add("cboClient", cboClient.SelectedValue);
            Context.Items.Add("txtSearch", txtSearch.Text);
        }

        loadData();
        searchProposal();
    }

    void InitLookUp()
    {
        object connStr = ConfigurationSettings.AppSettings["sqlConn"];
        using (SqlConnection conn = new SqlConnection(connStr.ToString()))
        {
            ptype_lookup = Lookup.Fill("sp_Select_cms_Lookup 'project_type'", conn);
            rcat_lookup = Lookup.Fill("sp_Select_cms_Lookup 'research_category_s'", conn);
        }
    }
    
    void loadData()
    {
        ListItem li = new ListItem("*-ALL-*", "0");
        
        List.fillList(cboClient, "SELECT [Company_ID], [Company_Name] FROM [vw_Clients] ORDER BY [Company_Name];");
        cboClient.Items.Insert(0, li);
        // load reminder count
        Mics.countReminder(lblB_ReminderCount, "Billing");
        Mics.countReminder(lblP_ReminderCount, "Proposal");
        // load postback data
        if (Context.Items.Count > 1)
        {
            string client = Context.Items["cboClient"].ToString();
            string search = Context.Items["txtSearch"].ToString();
            //Response.Write("client: " + client);
            cboClient.SelectedValue = client;
            txtSearch.Text = search;
        }
    }

    void searchProposal()
    {
        //int clientID = Convert.ToInt32(cboClient.SelectedValue);
        if (Context.Items.Count > 1)
        {
            menu_structure = new StringBuilder(512);
            int client = Convert.ToInt32(Context.Items["cboClient"]);
            string search = Context.Items["txtSearch"].ToString();
            string sqlStr = "SELECT [vw_Proposals].*, ISNULL([cms_Projects].[ProposalID], 0) AS [Confirm_pid] FROM [vw_Proposals] LEFT OUTER JOIN [cms_Projects] ON [vw_Proposals].[ProposalID]= [cms_Projects].[ProposalID] WHERE [ProjectName] LIKE '%" + search + "%' ";
            if (client > 0)
                sqlStr += " AND [ClientID]=" + client.ToString();
            object connStr = ConfigurationSettings.AppSettings["sqlConn"];
            using (SqlConnection conn = new SqlConnection(connStr.ToString()))
            {
                SqlCommand cmd = new SqlCommand(sqlStr, conn);
                conn.Open();
                SqlDataReader dr = cmd.ExecuteReader();
                grid1.DataSource = dr;
                grid1.DataBind();
            }
            // Register menu_structure
            RegisterClientScriptBlock("menu_structure", "<script>" + menu_structure.ToString() + "</" + "script>");
        }
    }

    protected void grid1_ItemDataBound(object sender, DataGridItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
        {
            int ind = e.Item.ItemIndex;
            string pid = e.Item.Cells[0].Text;
            string oid = e.Item.Cells[1].Text;
            string confirm_pid = e.Item.Cells[2].Text;

            menu_structure.Append("var menu" + ind + "=new Array(); ");
            menu_structure.Append("menu" + ind + "[0]=\'<a href=\"listAssociatedFiles.aspx?pid=" + pid + "\">Associated Files</a>\'; ");            

            if (confirm_pid == "0")
            {
                menu_structure.Append("menu" + ind + "[1]=\'<a href=\"saveProject.aspx?pid=" + pid + "&confirm=1\">Confirm the proposal</a>\'; ");
            }
            else
            {
                menu_structure.Append("menu" + ind + "[1]=\'<a href=\"listContactPersons.aspx?pid=" + pid + "&oid=" + oid + "\">Contact Persons</a>\'; ");
                menu_structure.Append("menu" + ind + "[2]=\'<a href=\"listMilestones.aspx?pid=" + pid + "\">Billing Notes</a>\'; ");
            }
        }
    }

    string editProposal(string pid, string projectName, string confirm_pid)
    {
        if (pid == confirm_pid)
        {
            return "<a href=\"saveProject.aspx?pid="+ pid +"\">"+ projectName +"</a>";
        }
        else
        {
            return "<a href=\"saveProposal.aspx?pid="+ pid +"\">"+ projectName +"</a>";
        }
    }
</script>

<html xmlns="http://www.w3.org/1999/xhtml">

<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />

    <title>Indochina Research</title>
    <link rel="SHORTCUT ICON" href="../irl.ico" type="image/x-icon" />
    <link type="text/css" rel="stylesheet" href="../css/global.css" />
	<style>
	    /* Dropdown Menu */
        #dropmenudiv{	
	        position:absolute;
	        background-color: #FFFFFF;/*E0E0EE;*/
	        border:1px solid #655BA6;
	        line-height:18px;
	        z-index:10;
        }
        #dropmenudiv span{
	        display:block;
	        text-indent: 10px;
	        padding: 3px;
        }
        #dropmenudiv a{
	        display:block;
	        text-indent: 10px;
	        padding: 3px;
	        /*font-weight:bold;*/
	        text-decoration: none;
	        color:#000080;
        }
        #dropmenudiv a:hover{
	        color:#FFFFFF;
	        background-color: #000080;
        }

        /* Copyright */
        .copyright{
	        font-size:7.5pt;
	        color:#10147e;
        }
	</style>
	<script language="javascript" src="includes/dropdown.js"></script>

</head>

<body>
<table width="773" border="0" cellspacing="0" cellpadding="0" align="center">
	<tr>
		<td width="765" height="540" align="center" valign="top" style="border:solid 1px #10147e;" >
			<!-- header -->
			<table border="0" cellpadding="0" cellspacing="0" width="100%">
				<tr bgcolor="#F4F4F9">
					<td colspan="3" align="right" style="padding:10px;">
						<b class="blueL">Client Management System</b>
					</td>
				</tr>
				<tr>
					<td class="wave_l">&nbsp;</td>
					<td><img src="images/wave_logo.jpg" width="107" height="83"></td>
					<td class="wave_r">&nbsp;</td>
				</tr>
				<tr>
					<td colspan="3" style="padding:0 20px;">
						<div style="background-color:#bcb3d5; padding:5px 10px;">
							<span style="float:right">

								&nbsp;</span>
							<span class="pathmap">
								<a href="default.aspx" >CPanel</a> :: Ops Files
						  </span>
						</div>
						<br />
						
						
					</td>
				</tr>
				
			</table>
			
			<!-- body -->
			<table border="0" width="100%" cellspacing="0" cellpadding="0">
				<tr>
					<td align="center" valign="top" style="padding-left:20px;">
						<table align="left" border="0" cellspacing="0" cellpadding="0">
							<tr>
								<td valign="top">
									<form id="SearchForm" method="post" runat="server">
										<div id="sidebar" class="sidebar">
											<h3>Ops Files</h3>
											<div class="block">
												<div><h5>Search</h5></div>
												<div>Client : <asp:DropDownList ID="cboClient" runat="server" style="width:160px;" /></div>
												<div>Project : <asp:TextBox ID="txtSearch" runat="server" /> <asp:ImageButton ID="cmdSearch" ImageAlign="absMiddle" ImageUrl="images/find.gif" CommandName="Search" runat="server" /><!--<a href="javascript:doSearch();"><img align="absmiddle" src="images/find.gif" /></a>--></div>
												<div align="center"><a class="blue" href="saveProposal.aspx">Add New Proposal</a></div>
											</div>
											<div class="block">
												<div><h5>Reminders</h5></div>
												<div class="item"><a class="blue" href="listReminders.aspx">Billing Reminder <asp:Literal ID="lblB_ReminderCount" runat="server" /></a></div>
												<div class="item"><a class="blue" href="listReminders_P.aspx">Proposal Reminder <asp:Literal ID="lblP_ReminderCount" runat="server" /></a></div>
											</div>
											<div class="block">
												<div><h5>Reports</h5></div>
												<div class="item"><a class="blue" href="ops_Proposals.aspx">Proposal &amp; Confirmed Projects</a></div>
												<div class="item"><a class="blue" href="project_Timeline.aspx">Projects Timeline</a></div>
												<div class="item"><a class="blue" href="listArchivedProjects.aspx">Historical Data</a></div>
												<div class="item"><a class="blue" href="listArchivedProposals.aspx">Archived proposals</a></div>
											</div>
										</div>
									</form>
								</td>
								<td width="10"><img src="images/spacer.gif" width="10" height="1" /></td>
								<td valign="top">
									<!-- data row -->
                                    <div style="width:480px; overflow:auto; overflow-y:visible">
                                        <asp:DataGrid CssClass="data" ID="grid1" Width="100%" runat="server" CellSpacing="1" CellPadding="4"
                                            BorderWidth="0" GridLines="none" AutoGenerateColumns="false" OnItemDataBound="grid1_ItemDataBound">
                                            <HeaderStyle BackColor="#bcb3d5" Font-Size="7.5pt" ForeColor="white" Font-Bold="true" />
                                            <ItemStyle BackColor="#efefef" />
                                            <AlternatingItemStyle BackColor="white" />
                                            <Columns>
                                                <asp:BoundColumn DataField="ProposalID" Visible="false" />
                                                <asp:BoundColumn DataField="ClientID" Visible="false" />
                                                <asp:BoundColumn DataField="Confirm_pid" Visible="false" />
                                                
                                                <asp:BoundColumn HeaderText="ClientName" DataField="ClientName">
                                                    <HeaderStyle Width="180" />
                                                </asp:BoundColumn>
                                                
                                                <asp:TemplateColumn HeaderText="Project Name">
                                                    <HeaderStyle Width="180" />
                                                    <ItemTemplate>
                                                        <img src="images/dropdown.gif" align="right" style="cursor:pointer;" onclick="dropdownmenu(this, event, menu<%# Container.ItemIndex %>, '150px')" onMouseOut="delayhidemenu()" />
                                                        <%# editProposal(DataBinder.Eval(Container.DataItem, "ProposalID").ToString(), DataBinder.Eval(Container.DataItem, "ProjectName").ToString(), DataBinder.Eval(Container.DataItem, "Confirm_pid").ToString()) %>
                                                        
                                                    </ItemTemplate>
                                                </asp:TemplateColumn>
                                                
                                                <asp:BoundColumn HeaderText="Value" DataField="Costs" DataFormatString="{0:$#}">
                                                    <HeaderStyle Width="50" />
                                                </asp:BoundColumn>
                                                
                                                <asp:TemplateColumn HeaderText="Type">
                                                    <HeaderStyle Width="25" />
                                                    <ItemTemplate>
                                                        <%# ptype_lookup[DataBinder.Eval(Container.DataItem, "ProjectType").ToString()]%>
                                                    </ItemTemplate>
                                                </asp:TemplateColumn>
                                                
                                                <asp:TemplateColumn HeaderText="Sector">
                                                    <HeaderStyle Width="25" />
                                                    <ItemTemplate>
                                                        <%# rcat_lookup[DataBinder.Eval(Container.DataItem, "ResearchCategory").ToString()]%>
                                                    </ItemTemplate>
                                                </asp:TemplateColumn>
                                                
                                            </Columns>
                                        </asp:DataGrid>
                                    </div>
								</td>
							</tr>
						</table>
					</td>
				</tr>
			</table>
			
		</td>
		
		<td width="6" class="rightShadow" valign="top">
			<img src="images/white.gif" width="6" height="6" /></td>
	</tr>
	
	<!-- bottom shadow -->
	<tr>
		<td height="6" class="bottomShadow">
			<img src="images/white.gif" width="6" height="6" /></td>
		
		<td width="6" height="6">
			<img src="images/shadow_corner.gif" width="6" height="6" /></td>
	</tr>
</table>


</body>
</html>
